#include "../include/MainHeader.h"


int unmount_vfs(char *vfsLabel)
{



int i=0;


printf("fdcounter= %ld\n",fdcounter );

header.noOfUsedFileDescriptor=fdcounter;

FILE *fp;
int l;



fp=fopen(vfsLabel,"rb+");
if(fp==NULL)
	return 1;

	
fclose(fp);


if(ismounted!=1)
	return 4;


//printf("in unmount used fd=%d\n",header.noOfUsedFileDescriptor);
t_node* root=naryRoot;

//if(root!=NULL)
	//printf("\nroot data in unmount=%s\n",root->data->file_name);

fd fdarray[header.maxNoOfFileDescriptor-1];


	for(l=0;l<header.maxNoOfFileDescriptor-1;l++)
	{	
		strcpy(fdarray[l].file_name,"empty1");
		strcpy(fdarray[l].path,"empty1");
		fdarray[l].fileType=' ';
		fdarray[l].fileSize=0;
		fdarray[l].blockNum=0;



	}

//header.noOfUsedFileDescriptor=fdcounter;

//printtree(naryRoot);

int status=write_desc(root,fdarray,i);

//printf("vfslabel = %s\n",vfsLabel );
fp=fopen(vfsLabel,"rb+");
if(fp==NULL)
	return 1;
else
	{
		fwrite(&header,sizeof(header),1,fp);
		offset=0;
		fseek(fp,offset,SEEK_SET);
		char c;
		while(c!='#')
		{
			fread(&c,sizeof(c),1,fp);
			offset++;
		}
		//printf("offset=%ld\n",offset );
		int newoffset=(offset+sizeof(fd));

		fseek(fp,newoffset,SEEK_SET);

		fwrite(fdarray,((header.maxNoOfFileDescriptor-1)*sizeof(fd)),1,fp);
		/*char g;
		fread(&g,sizeof(char),1,fp);
		printf("char after unmount=%c\n",g );*/

	}



fclose(fp);



/*
printf("\n----------------fdarray contents----------\n");
for(i=0;i<10;i++)
{
	printf("filename[%d]= %s\n",i,fdarray[i].file_name);
	printf("filepath[%d]= %s\n",i,fdarray[i].path);
	printf("filetype[%d]= %c\n",i,fdarray[i].fileType);
	printf("blocknum[%d]= %d\n",i,fdarray[i].blockNum);



}


printf("\nUnmounted . .!!\n");
*/

/*

FILE *fp2;
fp2=fopen(vfsLabel,"rb");
char c='!';
offset=0;
while(c!='#')
		{
			fread(&c,sizeof(c),1,fp);
			offset++;
		}

fseek(fp2,offset,SEEK_SET);
struct FileDescriptor
 arrayfd3[header.maxNoOfFileDescriptor];
fread(arrayfd3,sizeof(fdarray),1,fp2);


int d;

for(d=0;d<10;d++){

printf("filename[%d]= %s\n",d,arrayfd3[d].file_name);
	printf("filepath[%d]= %s\n",d,arrayfd3[d].path);
	printf("filetype[%d]= %c\n",d,arrayfd3[d].fileType);
	printf("blocknum[%d]= %d\n",d,arrayfd3[d].blockNum);

}




fclose(fp2);


*/

free(naryRoot);
free(bstRoot);
free(hash);

//printf("\n-------------------End of Unmount...!!--------------\n");



ismounted=0;
return 0;


}


int write_desc(t_node *root,fd fdarray[],int j)
{

//	printf("in write desc , filename=%s\n",filename );

//	printf("%s",root->data->file_name);
/*
	if(count==0)
	{
		strcpy(name,filename);
		fp=fopen(name,"rb+"); //enter file open mode.
		if(fp==NULL)
			return 1;
		else
		fseek(fp,offset,SEEK_SET); //set pointer of file to location where to write descriptor.
			//printf("\nroot in count 0=%s\n",root->data->file_name);
	}

*/





	if(root!=NULL && root->is_deleted!=1)
	{
		//printf("In if\n");

		fd *f=(fd *) malloc(sizeof(fd));
		f = root->data;
				
		//printf("value of fd= %s\n",f->file_name);
		//fwrite(f, sizeof(struct FileDescriptor ), 1, fp);
		if(j!=0)
		{	fdarray[j-1]=*f;
		
		}
			
	}

	if(root!=NULL)
	{	j++;
		if(root->left_child!=NULL)
	  	    write_desc(root->left_child,fdarray,j);
	  	if(root->right_siblings!=NULL)
	    	write_desc(root->right_siblings,fdarray,j);
    	}


}
